package com.hh.workflow.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hh.baseframework.util.check.Check;
import com.hh.baseframework.util.date.DateFormat;
import com.hh.workflow.mode.User;
import com.hh.workflow.service.inf.IUserService;

@Service
public class WorkFlowUtil {
	@Autowired
	private IUserService userService;

	public Map<String, Object> taskToMap(Task task) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", task.getId());
		map.put("name", task.getName());
		map.put("pdid", task.getProcessDefinitionId());
		map.put("activityId", task.getTaskDefinitionKey());
		map.put("pdkey", task.getProcessDefinitionId().substring(0, 36));
		map.put("createTime", task.getCreateTime());
		map.put("piid", task.getProcessInstanceId());
		map.put("pdid", task.getProcessDefinitionId());
		return map;
	}

	public Map<String, Object> taskToMap(HistoricTaskInstance task) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", task.getId());
		map.put("name", task.getName());
		map.put("pdid", task.getProcessDefinitionId());
		map.put("activityId", task.getTaskDefinitionKey());
		map.put("pdkey", task.getProcessDefinitionId().substring(0, 36));
		map.put("createTime", task.getStartTime());
		map.put("endTime", task.getEndTime());
		Long dur = task.getDurationInMillis();
		if (task.getDurationInMillis() == null) {
			dur = System.currentTimeMillis() - task.getStartTime().getTime();
		}
		map.put("durationInMillis", DateFormat.millisecondTOHHMMSS(dur));
		map.put("piid", task.getProcessInstanceId());
		map.put("pdid", task.getProcessDefinitionId());
		map.put("assignee", task.getAssignee());
		if (Check.isNoEmpty(task.getAssignee())) {
			User user = userService.findUserById(task.getAssignee());
			map.put("assigneeName", user.getText());
		}

		return map;
	}

}
